package com.nx.filter.tag;

import com.nx.Constant;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;


public class SyncProducer {

    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException, MQBrokerException {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new DefaultMQProducer("nx-group");
        // 如果网络不太好，可以设置一下超时时间
        producer.setSendMsgTimeout(10*1000);
        producer.setNamesrvAddr(Constant.MQ);
        // 发送失败的时候重试次数
        producer.setRetryTimesWhenSendFailed(2);
        producer.start();



        for (int i = 0; i < 10; i++) {
            Message msg = new Message("nx-user-tag-filter",
                    "B",
                    ("Hello RocketMQ B " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            producer.send(msg);
        }
        for (int i = 0; i < 10; i++) {
                Message msg = new Message("nx-user-tag-filter",
                    "A",
                    ("Hello RocketMQ A" + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            producer.send(msg);
        }


        producer.shutdown();
    }
}
